﻿using Abide_Halo_Library.Halo_2_Map;
using Abide_Halo_Library.Windows.Forms;
using System;
using System.Windows.Forms;

namespace Test_Application
{
    public partial class Form1 : Form
    {
        private IndexEntry SelectedEntry
        {
            get;
            set;
        }
        private Halo2Map Map
        {
            get;
            set;
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Initialize
            using (OpenFileDialog OpenDlg = new OpenFileDialog())
            {
                //Setup
                OpenDlg.Filter = "Halo 2 Map Files (*.map)|*.map";
                OpenDlg.Title = "Open Halo 2 Map...";

                //Show
                if (OpenDlg.ShowDialog() == DialogResult.OK)
                {
                    //Load...
                    if (Map != null)
                        Map.Dispose();
                    Map = new Halo2Map();
                    Map.LoadMap(OpenDlg.FileName);

                    //Setup UI
                    SetupUI();
                }
            }
        }
        
        private void tagBrowseButton_Click(object sender, EventArgs e)
        {
            //Initialize
            if(Map != null)
            using (TagSelectDialog TagDlg = new TagSelectDialog(Map))
            {
                //Setup
                TagDlg.Title = "Choose Tag";
                TagDlg.Description = "Select Tag.";
                TagDlg.SelectedEntry = Map.GetEntryByFilename(tagNameBox.Text);
                TagDlg.Sorting = Abide_Halo_Library.Windows.Forms.TagSorting.Hierarchical;

                //Show
                if(TagDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    //Set...
                    SelectedEntry = TagDlg.SelectedEntry;
                    tagNameBox.Text = SelectedEntry == null ? "Null" : string.Format("{0}.{1}", SelectedEntry.FileName, SelectedEntry.Class);
                }
            }
        }

        private void renameMapToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Prepare
            bool set = false;

            //Check...
            if(Map != null)
                using(NameDialog nameDlg = new NameDialog())
                {
                    //Setup
                    nameDlg.Title = "Rename Map...";
                    nameDlg.Description = "Map name:";
                    nameDlg.SelectedName = Map.Name;

                    //Open
                    Map.IO.OpenIO();

                    //Show
                    if (nameDlg.ShowDialog() == DialogResult.OK)
                    { Map.Name = nameDlg.SelectedName; set = true; }

                    //Close
                    Map.IO.CloseIO();
                }

            //Check
            if (set)
            {
                Map.Reload();
                SetupUI();
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Exit the application.
            Application.Exit();
        }

        private void SetupUI()
        {
            //Clear
            tagView.Nodes.Clear();
            renameMapToolStripMenuItem.Enabled = false;

            //Create Main Node
            TreeNode MainNode = new TreeNode(Map.Name);
            MainNode.Name = Map.Name;
            MainNode.Tag = -1;

            //Build
            Map.BuildTreeNodes(ref MainNode, TagSorting.Hierarchical);

            //Expand
            MainNode.Expand();

            //Add
            tagView.Nodes.Add(MainNode);
            tagView.Sort();

            //Enable
            renameMapToolStripMenuItem.Enabled = true;
        }
    }
}
